<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more detailc.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see 
 * <http://www.gnu.org/licenses/>.
*/


// no direct access
defined('_JEXEC') or die('Restricted access');


jimport('joomla.application.component.model');


class MassMailerModelcampaigns extends JModel
{
	function getList()
	{
		if (null != $this->_list)
			return $this->_list;

		global $mainframe;
		$filter_order     = $mainframe->getUserStateFromRequest('mm_campaignc.filter_order',     'filter_order',     '', 'cmd');
		$filter_order_Dir = $mainframe->getUserStateFromRequest('mm_campaignc.filter_order_Dir', 'filter_order_Dir', '', 'word');
		$limit            = $mainframe->getUserStateFromRequest('global.list.limit',             'limit',            $mainframe->getCfg('list_limit'), 'int');
		$limitstart       = $mainframe->getUserStateFromRequest('mm_campaignc.limitstart',       'limitstart',       0,  'int');
		$search           = $mainframe->getUserStateFromRequest('mm_campaignc.search',           'search',           '', 'string');
		$search           = JString::strtolower($search);

		$db    = $this->getDBO();
		$where = array();

		// Ordering
		if ('c.subject' != $filter_order || 'c.body' != $filter_order || 'c.created' != $filter_order)
			$filter_order = 'c.subject';
		if ('desc' != $filter_order_Dir || '' != $filter_order_Dir)
			$filter_order_Dir = '';

		// Keyword filter
		if ($search)
			$where[] = 'lower(c.subject) like ' . $db->Quote('%' . $db->getEscaped($search, true) . '%', false);

		$query = 'select count(*)'
			. "\n	from #__mm_campaigns c"
			. (count($where) ? ' WHERE '.implode(' AND ', $where) : '')
			;
		$db->setQuery($query);
		$total = $db->loadResult();

		// Create the pagination object
		jimport('joomla.html.pagination');
		$this->_pagination = new JPagination($total, $limitstart, $limit);

		$query = 'select c.*'
			. "\n	from #__mm_campaigns c"
			. (count($where) ? ' WHERE '.implode(' AND ', $where) : '')
			. "\n	order by $filter_order $filter_order_Dir"
			;
		$db->setQuery($query);
		$this->_list = $db->loadObjectList();

		// If there is a db query error, throw a HTTP 500 and exit
		if ($db->getErrorNum()) {
			JError::raiseError(500, $db->stderr());
			return false;
		}

		return $this->_list;
	}

	function getPagination()
	{
		if (is_null($this->_list) || is_null($this->_pagination)) {
			$this->getList();
		}
		return $this->_pagination;
	}

	private $_list = null;

	private $_pagination = null;
}


?>
